package com.heartech.prediciton_wx.controller.verify;

import com.heartech.prediciton_wx.utils.verify.SignatureUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/wxpublic")
public class WxVerifyController {
    /* 微信验证代码 */
//    @RequestMapping("/verify_wx_token")
    public void verifyWXToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain;charset=UTF-8");
        // 获取请求参数
        String msgSignature = request.getParameter("signature");
        String msgTimestamp = request.getParameter("timestamp");
        String msgNonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        try {
            // 验证签名
            if (SignatureUtil.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
                // 如果验证成功，将echostr写入响应体
                response.getWriter().write(echostr);
            }
        } catch (Exception e) {
            // 处理异常，例如记录日志等
            e.printStackTrace();
        }
    }
}